package com.ikun.controller;

import com.ikun.pojo.Result;
import com.ikun.service.ShoppingCartService;
import com.ikun.service.impl.ShoppingCartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    //根据businessId与userId查询购物车
    @GetMapping("/getShoppingCart")
    public Result getShoppingCart(@RequestParam("businessId") Integer businessId, @RequestParam("userId") Integer userId){
        // TODO: Implement the logic to fetch shopping cart based on businessId and userId
        // For demonstration purposes, we'll return a sample result
        return Result.success(shoppingCartService.getShoppingCartByBusinessIdAndUserId(businessId, userId));

    }
    //根据userId,businessId,quantity,DishesId添加购物车
    @GetMapping("/addShoppingCart")
    public Result addShoppingCart(@RequestParam("userId") Integer userId, @RequestParam("businessId") Integer businessId,
                                  @RequestParam("quantity") Integer quantity, @RequestParam("dishesId") Integer dishesId){
        int result=shoppingCartService.addShoppingCart(userId,businessId,quantity,dishesId);
        if(result>0){
            return Result.success(null);
        }else{
            return Result.error("添加购物车失败");
        }
    }
    //根据userId,businessId,DishesId修改购物车中quantity的数量
    @GetMapping("/updateQuantity")
    public Result updateQuantity(@RequestParam("userId") Integer userId, @RequestParam("businessId") Integer businessId,
                                  @RequestParam("dishesId") Integer dishesId, @RequestParam("quantity") Integer quantity){
        int result=shoppingCartService.updateQuantityByUserIdAndBusinessIdAndDishesId(userId,businessId,dishesId,quantity);
        if(result>0){
            return Result.success(null);
        }else{
            return Result.error("修改购物车失败");
        }
    }
    //根据userId,businessId,DishesId删除购物车中该项
    @GetMapping("/deleteShoppingCart")
    public Result deleteShoppingCart(@RequestParam("userId") Integer userId, @RequestParam("businessId") Integer businessId,
                                  @RequestParam("dishesId") Integer dishesId){
        int result=shoppingCartService.deleteShoppingCartByUserIdAndBusinessIdAndDishesId(userId,businessId,dishesId);
        if(result>0){
            return Result.success(null);
        }else{
            return Result.error("删除购物车失败");
        }
    }



}
